package persentation.presentAdministrator;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularType;
import javax.print.DocFlavor.STRING;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.DefaultTableModel;

import vo.courseVO.CourseVO;
import vo.userVO.UserVO;
import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.courseBl.CourseController;
import businesslogic.userBl.Administrator;
import businesslogic.userBl.AdministratorController;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import dataservice.ResultMessage;

public class DeleteUserPanel extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<UserVO> userList;
	private JTable table;
	private ShowUserPanel showUserPanel;
	private DefaultTableModel model;
	private JComboBox<String> userTypeBox;
	private JComboBox<String> departmentBox;
	private JButton deleteButton;
	private JButton selectAllButton;
	private ArrayList<ResultMessage> resultMessageList=new ArrayList<>();
	
	private int userType;
	private String departmentID="全部";
	private String []departmentList=new String[29];
	
	
	private AdministratorController administratorController=new AdministratorController();

	/**
	 * Create the panel.
	 */
	
	public DeleteUserPanel(CourseController courseController) {
		

		
		
		setSize(800, 600);
		setLayout(null);
		
		ArrayList<String> departmentArrayList;
		try {
			departmentArrayList = courseController.getAllDepartments();
			departmentList[0]="全部";
			for (int i = 0; i<departmentArrayList.size(); i++) {
				departmentList[i+1]=departmentArrayList.get(i);
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}
		
		
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(37, 93, 712, 384);

		this.add(scrollPane);
		
		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		table.setModel(new DefaultTableModel(
			new Object[][] {
//				
			},
			new String[] {
					 "用户ID","用户名","删除用户"
			}
		) 
		{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			Class[] columnTypes = new Class[] {
				String.class, String.class,Boolean.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			
			public boolean isCellEditable(int row, int column)
			{
				
				if(column==2)
					return true;
			return false;//父类的方法里面是 return true的，所以就可以编辑了~~~
			}
		});
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if(table.getSelectedRow()!=-1)
					deleteButton.setEnabled(true);
				
			}
		});
//		table.setBounds(119, 424, 262, 143);
		
		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(false);
		scrollPane.setViewportView(table);
		

		
		userType=0;
		departmentID="全部";
		fillTable();
		table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		deleteButton = new JButton("删除");
		deleteButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int message=JOptionPane.showConfirmDialog(null,"确认删除用户？","确认",JOptionPane.YES_NO_OPTION);
				if(message==JOptionPane.YES_OPTION){
					ResultMessage msgMessage=sendRequest();
					System.out.println("+++++++++++++++++"+msgMessage);
					if(msgMessage==ResultMessage.DELETE_USER_SUCCESS){
						JOptionPane.showMessageDialog(null,"删除用户成功！","提示",JOptionPane.INFORMATION_MESSAGE);

						showUserPanel.fillTable();
						fillTable();
						setEnabled(false);
					}
					
					else
						JOptionPane.showMessageDialog(null,"删除用户失败！","提示",JOptionPane.INFORMATION_MESSAGE);
					
					
				}
			}
		});
		deleteButton.setEnabled(false);
		deleteButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		deleteButton.setBounds(177, 487, 93, 38);
		add(deleteButton);
		
		selectAllButton = new JButton("反选");
		selectAllButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				deleteButton.setEnabled(true);
				int row=table.getRowCount();
				for(int i=0;i<row;i++)
					table.setValueAt(!(boolean)table.getValueAt(i, 2), i, 2);
				
			}
		});
		
		
		
		selectAllButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		selectAllButton.setBounds(480, 487, 82, 38);
		add(selectAllButton);
		
		JLabel lblNewLabel = new JLabel("用户类型");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		lblNewLabel.setBounds(48, 26, 127, 38);
		add(lblNewLabel);
		
		userTypeBox = new JComboBox();
		userTypeBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
//		userTypeBox.addItemListener(new ItemListener() {
//			
//			@Override
//			public void itemStateChanged(ItemEvent e) {
//				if(e.getStateChange()==ItemEvent.SELECTED)
//					userType=userTypeBox.getSelectedIndex();	
//				
//					fillTable();
//			}
//		});
		userTypeBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(userTypeBox.getSelectedIndex()==3){
					departmentBox.setSelectedIndex(0);
					departmentBox.setEditable(false);
					
					
				}
					
				
				userType=userTypeBox.getSelectedIndex();	
				
				fillTable();
			}
		});
		userTypeBox.setModel(new DefaultComboBoxModel<String>(new String[]{"学生","任课教师","院系教务老师","教务处老师"}));
		userTypeBox.setBounds(190, 30, 177, 30);
		add(userTypeBox);
		
		JLabel label = new JLabel("院系名称");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		
		label.setBounds(423, 26, 87, 38);
		add(label);
		
		departmentBox = new JComboBox();
		departmentBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		departmentBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange()==ItemEvent.SELECTED) {
					departmentID=departmentBox.getSelectedItem().toString();
					fillTable();
				}
			}
		});
		departmentBox.setModel(new DefaultComboBoxModel(departmentList));
		departmentBox.setBounds(542, 30, 177, 30);
		add(departmentBox);
		
		
		
		
//		table.setBackground(new Color(135, 206, 250));
//		table.add(table.getTableHeader());
		
//		fillTable();

	}
	
	public void fillTable(){
		
		
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		 tableModel.setRowCount(0);
		 try {
			userList=administratorController.getUserList(userType, departmentID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ExceptionHandler.networkException();
		}
		 
		 for(UserVO userVO:userList){
			 Object[] array=new Object [4];
			 array[0]=String.valueOf(userVO.getId());
			 array[1]=String.valueOf(userVO.getName());
			
			 array[2]=false;
			
			
			 System.out.println(array[0]);
			 System.out.println(array[1]);
			 System.out.println(array[2]);
			 tableModel.addRow(array);
			 table.invalidate();
			 table.updateUI();
	      }
	} 
	
	
	public ResultMessage sendRequest(){
		resultMessageList.clear();;
		ResultMessage msg=ResultMessage.DELETE_USER_SUCCESS;
		int row=table.getRowCount();
		for(int i=0;i<row;i++){
			if((boolean)table.getValueAt(i, 2)==true){
				System.out.println("++++++++++++++++++++++++");
				System.out.println(table.getValueAt(i, 0).toString().trim());
				System.out.println(table.getValueAt(i, 1).toString().trim());
				try {
					msg=administratorController.deleteUser(new UserVO(table.getValueAt(i, 0).toString().trim(), table.getValueAt(i,1).toString().trim()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					ExceptionHandler.networkException();
				}
				
				
				resultMessageList.add(msg);
			}
				
		}
		
		for(ResultMessage m:resultMessageList)
			if(m==ResultMessage.DELETE_USER_FAILURE)
				msg=ResultMessage.DELETE_USER_FAILURE;
		
		
		return msg;
	}
	
	public void setShowPanel(ShowUserPanel showUserPanel){
		this.showUserPanel=showUserPanel;
	}
	
}
